package com.csxy.six.controller;

import com.csxy.six.R;
import com.csxy.six.entity.ReadLog;
import com.csxy.six.service.ReadLogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 阅读日志控制器
 * 提供阅读记录相关的RESTful API
 */
@RestController
@RequestMapping("/annoservice/read-log")
@Tag(name = "阅读记录", description = "提供公告阅读记录的添加和查询接口")
public class ReadLogController {

    @Autowired
    private ReadLogService readLogService;

    @Operation(summary = "记录阅读日志")
    @PostMapping
    public R recordRead(@RequestParam Integer userId,
                        @RequestParam Integer announceId) {
        return readLogService.recordRead(userId, announceId) ? R.ok() : R.error();
    }

    @Operation(summary = "获取用户阅读记录")
    @GetMapping("/user/{userId}")
    public R getByUserId(@PathVariable Integer userId) {
        return R.ok().data("list", readLogService.lambdaQuery()
                .eq(ReadLog::getUserId, userId)
                .list());
    }

    @Operation(summary = "获取公告阅读记录")
    @GetMapping("/announcement/{announceId}")
    public R getByAnnounceId(@PathVariable Integer announceId) {
        return R.ok().data("list", readLogService.lambdaQuery()
                .eq(ReadLog::getAnnounceId, announceId)
                .list());
    }
}